#!/usr/bin/perl -w # for syntax highlighting
use gri_database;

sub mm_form_docrepos_admin_add_group_populate {
my $group_name=shift;
my $group_description=shift;

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	$group_name="" if $group_name eq "?group_name - not defined?";
	$group_description="" if $group_description eq "?group_description - not defined?";

	$group_name=hex_to_string($group_name);
	$group_description=hex_to_string($group_description);

my $s="
    <tr class=gri_headerrow>
      <td class=gri_cell colspan=3>
        <font class=\"redSub14\">New Document Repository Group Details</font>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Name:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"text\" VALUE=\"$group_name\" NAME=\"name\" SIZE=\"60\" MAXLENGTH=\"40\" style=\"font-family:Arial; font-size:11px; background-color: #FFFFFF; border:1px double #E0E0E0;\">
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Description:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <TEXTAREA NAME=\"description\" COLS=60 ROWS=5 style=\"font-family:Arial; font-size:11px; background-color: #FFFFFF; border:1px double #E0E0E0;\">$group_description</TEXTAREA>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Group Members:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>\n";

	my $users=$::GRI_FRONTEND->query_authd("GET_USER_LIST");
	if(!defined($users)) {
		print STDERR "Error: mm_form_docrepos_admin_add_group_populate :: GET_USER_LIST request errored!\n";
		return "Error: mm_form_docrepos_admin_add_group_populate :: GET_USER_LIST errored!";
	} 
	
	my $row=0; my $col=0;
	$s.="<table cols=4 width=100%>\n";
	
	my @L=split(/\n/,$users);
	@L=sort {my @a2=split(/\|/,$a); my @b2=split(/\|/,$b); $a2[1] cmp $b2[1]} @L[1 .. $#L];
	my ($x,$y)=$DB->get_group(group=>$group_name);
	my @CG=();
	if($x==1) {
		@CG=split(/,/,$y->{users});
	}
	for my $crep (@L) {
		 my ($uid,$name)=split(/\|/,$crep);
		 # Assume authd filters out users we do not want
		 if($col==0) {
		 	if($row!=0) {
				$s.="</tr>\n";
			}
			$s.="<tr>\n";
		}
		$s.="<td align=left valign=left style=\"font-family:arial; font-size: 80%\">";
		if(grep {$uid eq $_} @CG) {
			$s.="<input type=checkbox name=\"user_$uid\" value=1 checked=yes>$name [$uid]<br>";
		 } else {
			$s.="<input type=checkbox name=\"user_$uid\" value=1>$name [$uid]<br>";
		 }
		 $s.="</td>\n";
		 $col++;
		 if($col>3) {
		 	$col=0;
			$row++;
		}
	}
	if($row+$col>0) {
		$s.="</tr>";
	}
	$s.="</table>\n";
	$s.="\n</td></tr>\n";
	return $s;
}

1;

# vim: ts=4:sw=4
